package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.base.BaseRequest;
import com.bbx.api.sdk.model.official.passanger.Officail_VehicleBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle.VehicleList;
import com.bbx.api.sdk.model.passanger.OrderDetailBuild;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.WeightOrVolumnBuild;
import com.bbx.api.sdk.model.passanger.Return.OrderDetails;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Locations;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_VehicleNet;
import com.bbx.api.sdk.net.passeger.conn.CancelReasonListNet;
import com.bbx.api.sdk.net.passeger.conn.OrderDetailNet;
import com.bbx.api.sdk.net.passeger.conn.WeightNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Task.DetailsOfGoodsTask.OnFinishListener;

/** 
 * @Path com.bbx.taxi.client.Task.VehicleTask.java
 * @Author 严旭文
 * @Date 2016-1-21 下午12:09:42
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 车辆列表
 */
public class MyVehicleTask {
	private Activity context;
	private MyApplication mApplication;
	public static MyVehicleTask instance;
	public VehiclelTask mVehiclelTask;
	public interface OnVehiclelListener {
		/**
		 *车辆列表，求完毕返回list为空则失败
		 */
		public void onVehiclel(ArrayList<VehicleList> list);
	}

	static OnVehiclelListener mOnVehiclelListener = null;

	public static void setOnVehiclelListener(OnVehiclelListener l) {
		mOnVehiclelListener = l;
	}
	public static MyVehicleTask getInstance(Activity context,
			MyApplication mApplication) {
		instance = new MyVehicleTask(context, mApplication);
		return instance;
	}
	public MyVehicleTask(Activity context, MyApplication mApplication) {
		this.context = context;
		this.mApplication = mApplication;
		getVehicle();
	}
	/**
	 * 获取订单详情 	
	 */
	public void getVehicle() {
		Officail_VehicleBuild mOfficail_VehicleBuild = new Officail_VehicleBuild(context);
		mOfficail_VehicleBuild.tc_id="1";
		mOfficail_VehicleBuild.pos=0;
		mOfficail_VehicleBuild.limit=100;
		mVehiclelTask = new VehiclelTask(context, mOfficail_VehicleBuild);
		mVehiclelTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	private class VehiclelTask extends BaseAsyncTask {

		private Officail_VehicleBuild mOfficail_VehicleBuild;

		public VehiclelTask(Activity context, Officail_VehicleBuild mOfficail_VehicleBuild) {
			super(context);
			setIsReturnString(true);
			this.mOfficail_VehicleBuild = mOfficail_VehicleBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				JSONObject json;
				JSONArray json_array = null;
				try {
					json = new JSONObject((String) object);
					json_array=json.getJSONArray("list");
				} catch (JSONException e1) {
					e1.printStackTrace();
				}
		
				
				Officail_Vehicle mOfficail_Vehicle = (Officail_Vehicle) new JsonBuild().getData(Officail_Vehicle.class,
						 (String)object);
				ArrayList<VehicleList> list=mOfficail_Vehicle.getList();
				if (mOfficail_Vehicle != null&&list!=null) {
					for(int i=0;i<list.size();i++){
						JSONObject jo;
						try {
							jo = new JSONObject(json_array.get(i)	.toString());
							list.get(i).setPrivates(jo.getString("private"));
						} catch (JSONException e) {
							e.printStackTrace();
						}
			
					}
					
					if (mOnVehiclelListener != null) {
						mOnVehiclelListener.onVehiclel(mOfficail_Vehicle.getList());
				}
				}

			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnVehiclelListener != null) {
					mOnVehiclelListener.onVehiclel(null);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, R.string.no_network);
			if (mOnVehiclelListener != null) {
				mOnVehiclelListener.onVehiclel(null);
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficail_VehicleBuild).getJson();
			return new Officail_VehicleNet(context, json);
		}
	}

}
